package chapter15;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/23 14:21
 */
class Tourist extends Thread {
    AssemblePoint point;

    public Tourist(AssemblePoint point) {
        this.point = point;
    }

    @Override
    public void run() {
        try {
            int time = new Random().nextInt(3);
            //先模拟各自独立运行
            TimeUnit.SECONDS.sleep(time);
            System.out.println(Thread.currentThread().getName() + " walking for " + time + " hours and arrive");
            //集合
            point.await();
            System.out.println("all arrive");
            //...其他操作
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public class AssemblePointDemo {
    public static void main(String[] args) throws InterruptedException {
        int num = 10;
        Tourist[] tourists = new Tourist[num];
        AssemblePoint ap = new AssemblePoint(num + 1);
        for (int i = 0; i < num; i++) {
            tourists[i] = new Tourist(ap);
            tourists[i].start();
        }
        ap.await();

        System.out.println("start for another point");
        ap = new AssemblePoint(num);
        for (int i = 0; i < num; i++) {
            tourists[i] = new Tourist(ap);
            tourists[i].start();
        }
    }
}
